//
//  GQLabelController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/6.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQLabelController.h"
#import "GQRegisterTool.h"
#import "GQRegisterLabelResult.h"
#import "GQRegisterTagInfo.h"
#import "MBProgressHUD.h"
#import "GQRegisterInfo.h"
#import "TipView.h"
#import "GQRegisterDetailController.h"
#import "GQLoginResult.h"
#import "GQLoginUserResult.h"
#import "GQProfileTool.h"

#define kLabelMargin 10
#define kLabelCount 3
#define kSelectCount 13


@interface GQLabelController ()

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) NSMutableArray *btnArrM;
@property (strong, nonatomic) NSMutableArray *coverArrM;
@property (strong, nonatomic) NSMutableArray *selectedArrM;
@property (strong, nonatomic) NSMutableArray *titleArrM;
@property (strong, nonatomic) NSMutableArray *tagArrM;

@end

@implementation GQLabelController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"标签";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"distribute_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backToPre)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *rightItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 20)];
    [rightItem setTitle:@"保存" forState:UIControlStateNormal];
    [rightItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightItem.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightItem addTarget:self action:@selector(saveItemDidClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItem];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [GQRegisterTool registerLabelInfo:^(NSArray *result) {
        _labelsArray = result;
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self setUpChildView];
    } failure:^(NSError *error) {
        
    }];
    [self.btnArrM removeAllObjects];
}

- (void)backToPre
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveItemDidClick
{
    if (self.btnArrM.count < 1) {
        [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:@"至少选择一个标签"];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:[GQLoginResult sharedGQLoginResult].user.userId forKey:@"userId"];
    [param setObject:[self.tagArrM componentsJoinedByString:@","] forKey:@"tags"];
    [GQProfileTool profileModifyUserInfo:param modifyType:@"1" success:^(id result) {
        NSInteger errorCode = [[result objectForKey:@"errorCode"] integerValue];
        NSLog(@"111 --- %d", errorCode);
        if (errorCode == 0) {
            [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_smile"] tipText:@"修改成功"];
        }else{
            [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:@"修改失败"];
        }
    } failure:^(NSError *error) {
        
    }];
    
    [GQRegisterInfo sharedGQRegisterInfo].label = self.btnArrM;
    [GQRegisterInfo sharedGQRegisterInfo].tag = self.tagArrM;
    [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_smile"] tipText:@"保存成功"];
    
}

- (void)setUpChildView
{
    
    CGFloat btnWidth = (kScreenWidth - (kLabelCount + 1) * kLabelMargin) / kLabelCount;
    CGFloat btnHeight = btnWidth;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    NSInteger cols = 0;
    NSInteger rows = 0;
    NSInteger count = self.labelsArray.count;
    CGFloat viewHeight = 0;
    
    for (NSInteger i = 0; i < count; i++) {
        GQRegisterTagInfo *tagInfo = self.labelsArray[i];
        rows = i % kLabelCount;
        cols = i / kLabelCount;
        btnX = kLabelMargin + rows * (btnWidth + kLabelMargin);
        btnY = kLabelMargin + cols * (btnHeight + kLabelMargin);
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnWidth, btnHeight)];
        btn.tag = i;
        [btn setImage:[UIImage imageNamed:tagInfo.tagUrl] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat imageWidth = 44;
        UILabel *coverView = [[UILabel alloc] initWithFrame:btn.bounds];
        coverView.backgroundColor = [UIColor blackColor];
        coverView.alpha = 0.5;
        [self.coverArrM addObject:coverView];
        [btn addSubview:coverView];
        
        UIButton *imageView = [[UIButton alloc] init];
        imageView.frame = CGRectMake((btn.width - imageWidth) / 2, (btn.width - imageWidth) / 2, imageWidth, imageWidth);
        [imageView setImage:[UIImage imageNamed:@"gq_tag_selected"] forState:UIControlStateNormal];
        imageView.alpha = 0;
        imageView.tag = i;
        [imageView addTarget:self action:@selector(tapGestureDidTap:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat titleWidth = 80;
        CGFloat titleHeight = 20;
        UILabel *titleView = [[UILabel alloc] init];
        titleView.frame = CGRectMake((btn.width - titleWidth) / 2, (btn.height - titleHeight) / 2, titleWidth, titleHeight);
        titleView.text = tagInfo.name;
        titleView.font = [UIFont systemFontOfSize:16];
        titleView.textColor = [UIColor whiteColor];
        titleView.textAlignment = NSTextAlignmentCenter;
        [self.titleArrM addObject:titleView];
        
        [self.selectedArrM addObject:imageView];
        [btn addSubview:imageView];
        [btn addSubview:titleView];
        [self.scrollView addSubview:btn];
        viewHeight = CGRectGetMaxY(btn.frame) + kLabelMargin;
    }
    self.scrollView.contentSize = CGSizeMake(kScreenWidth, viewHeight);
}

- (void)tapGestureDidTap:(UIButton *)img
{
    UIButton *imageView = self.selectedArrM[img.tag];
    UIButton *selectedBtn = (UIButton *)[imageView superview];
    [self btnDidClick:selectedBtn];
}

- (void)btnDidClick:(UIButton *)btn
{
    GQRegisterTagInfo *tagInfo = self.labelsArray[btn.tag];
    if ([self.btnArrM containsObject:[tagInfo name]]) {
        UILabel *coverView = self.coverArrM[btn.tag];
        coverView.alpha = 0.5;
        UIButton *imageView = self.selectedArrM[btn.tag];
        imageView.alpha = 0;
        UILabel *titleLabel = self.titleArrM[btn.tag];
        titleLabel.alpha = 1;
        NSInteger tag = pow(2, btn.tag);
        [self.tagArrM removeObject:[NSNumber numberWithInteger:tag]];
        [self.btnArrM removeObject:[tagInfo name]];
    }else if (self.btnArrM.count < kSelectCount) {
        UILabel *coverView = self.coverArrM[btn.tag];
        coverView.alpha = 0;
        UIButton *imageView = self.selectedArrM[btn.tag];
        imageView.alpha = 1;
        UILabel *titleLabel = self.titleArrM[btn.tag];
        titleLabel.alpha = 0;
        NSInteger tag = pow(2, btn.tag);
        [self.tagArrM addObject:[NSNumber numberWithInteger:tag]];
        [self.btnArrM addObject:[tagInfo name]];
    }
}

- (NSMutableArray *)btnArrM
{
    if (_btnArrM == nil) {
        _btnArrM = [NSMutableArray array];
    }
    return _btnArrM;
}

- (NSMutableArray *)coverArrM
{
    if (_coverArrM == nil) {
        _coverArrM = [NSMutableArray array];
    }
    return _coverArrM;
}

- (NSMutableArray *)selectedArrM
{
    if (_selectedArrM == nil) {
        _selectedArrM = [NSMutableArray array];
    }
    return _selectedArrM;
}

- (NSMutableArray *)titleArrM
{
    if (_titleArrM == nil) {
        _titleArrM = [NSMutableArray array];
    }
    return _titleArrM;
}

- (NSMutableArray *)tagArrM
{
    if (_tagArrM == nil) {
        _tagArrM = [NSMutableArray array];
    }
    return _tagArrM;
}

@end
